Contents

转:http://www.cnblogs.com/rollenholt/archive/2011/06/03/2070587.html

  类Arrays提供静态方法asList。将数组当做一个List的集合。其中封装了和链表相似的行为、

  此方法同 Collection.toArray() (浅拷贝)一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。

asList
public static List asList(T… a)
返回一个受指定数组支持的固定大小的列表。(对返回列表的更改会“直接写”到数组。)此方法同 Collection.toArray() 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。
此方法还提供了一个创建固定长度的列表的便捷方法,该列表被初始化为包含多个元素:

1
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

  • 参数:
    a - 支持列表的数组。
  • 返回:
    指定数组的列表视图。

  Arrays.asList创建一个固定大小的List,他的操作速度比任何已有的List的实现都要快。但是如果对其调用add或者remove方法将会抛出异常:UnsupportedOpterationException。

  java.util.Arrays.asList的定义,函数参数是Varargs, 采用了泛型实现(泛型不支持基本类型,如果是int[]则会转为只有一个元素的List,int[]数组是对象可以泛型化,解决方法改为Integer[])。同时由于autoboxing的支持,使得可以支持对象数组以及基本类型数组。

不过在使用时,当传入基本数据类型的数组时,会出现小问题,会把传入的数组整个当作返回的List中的第一个元素,例如:

1
2
3
4
5
6
7
8
9
10
 public static void main(String[] args){  
int[] a1 = new int[]{1,2,3};
String[] a2 = new String[]{"a","b","c"};

System.out.println(Arrays.asList(a1));
System.out.println(Arrays.asList(a2));
}
打印结果如下:
[[I@dc8569]
[a, b, c]

下面说说Arrays.asList()的返回值:
JDK文档是这么说的:

  public static List asList(T… a) 返回一个受指定数组支持的固定大小的列表。(对返回列表的更改会“直接写”到数组。)此方法同 Collection.toArray() 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。此方法还提供了一个创建固定长度的列表的便捷方法,该列表被初始化为包含多个元素: List stooges = Arrays.asList(“Larry”, “Moe”, “Curly”);

  我们都知道,List的一个典型的特性就是其长度是可变的,我们可以很方便地对它进行插入和删除元素的操作,这是它与数组所存在的一个很大的区别,后者的长度是固定的,而且我们不能从数组中删除元素,只能修改元素的值。

  利用Arrays.asList(array)将返回一个List,然而这个返回的List并不支持add和remove的操作。

这是什么原因呢?
Arrays.asList源码:

1
2
3
4
 
1 public static <T> List<T> asList(T... a) {
2 return new ArrayList<T>(a);
3 }

这里的ArrayList并不是java.util.ArrayList,而是Arrays的内部类:
只实现了size,toArray,get,set,contains,indexOf方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 1     /**
2 * @serial include
3 */

4 private static class ArrayList<E> extends AbstractList<E> implements
5 RandomAccess, java.io.Serializable {

6 private static final long serialVersionUID = -2764017481108945198L;
7 private final E[] a;
8
9 ArrayList(E[] array) {
10 if (array == null)
11 throw new NullPointerException();
12 a = array;
13 }
14
15 public int size() {
16 return a.length;
17 }
18
19 public Object[] toArray() {
20 return a.clone();
21 }
22
23 public <T> T[] toArray(T[] a) {
24 int size = size();
25 if (a.length < size)
26 return Arrays.copyOf(this.a, size, (Class<? extends T[]>) a
27 .getClass());
28 System.arraycopy(this.a, 0, a, 0, size);
29 if (a.length > size)
30 a[size] = null;
31 return a;
32 }
33
34 public E get(int index) {
35 return a[index];
36 }
37
38 public E set(int index, E element) {
39 E oldValue = a[index];
40 a[index] = element;
41 return oldValue;
42 }
43
44 public int indexOf(Object o) {
45 if (o == null) {
46 for (int i = 0; i < a.length; i++)
47 if (a[i] == null)
48 return i;
49 } else {
50 for (int i = 0; i < a.length; i++)
51 if (o.equals(a[i]))
52 return i;
53 }
54 return -1;
55 }
56
57 public boolean contains(Object o) {
58 return indexOf(o) != -1;
59 }
60 }

我们可以看到该内部类继承的是AbstractList,下面是AbstractList的add和remove方法源码:

1
2
3
4
5
6
7
8
9
10
11
12
 1     public boolean add(E e) {
2 add(size(), e);
3 return true;
4 }
5
6 public void add(int index, E element) {
7 throw new UnsupportedOperationException();
8 }
9
10 public E remove(int index) {
11 throw new UnsupportedOperationException();
12 }
  1. 所以,当我们对Arrays.asList返回的List进行添加或删除时将会报 java.lang.UnsupportedOperationException 异常。
    而且对asList产生的List的修改,会涉及到原来的数组。如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    1.	package AsList;  
    2.
    3. import java.util.Arrays;
    4. import java.util.List;
    5.
    6. public class AsList {
    7. private String[] num = { "blue", "red", "yellow" };
    8. private int[] data = { 1, 2, 3 };
    9. private List list;
    10. private List list1;
    11.
    12. public AsList() {
    13. list = Arrays.asList(num);
    14. list1 = Arrays.asList(data);
    15. }
    16.
    17. public void printnumber() {
    18. for (int i = 0; i < list1.size(); ++i)
    19. System.out.println(list1.get(i));
    20. for (int i = 0; i < list.size(); ++i)
    21. System.out.println(list.get(i));
    22. list.set(1, "green");
    23. for (int i = 0; i < list.size(); ++i)
    24. System.out.println(list.get(i));
    25. for (int i = 0; i < num.length; ++i)
    26. System.out.println(num[i]);
    27.
    28. }
    29.
    30. public static void main(String[] args) {
    31. new AsList().printnumber();
    32. }
    33.
    34. }
    输出结果为:
    [I@c17164
    blue
    red
    yellow
    blue
    green
    yellow
    blue
    green
    yellow
Contents